package com.example.myapplication;

import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    FragmentManager fm;
    MyFragment fm1 ;
    MyFragment2 fm2;
    FatherFragment father;
    Button a2f1,a2f2,back,goin;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        a2f1 =findViewById(R.id.button1);
        a2f2=findViewById(R.id.button2);
        back = findViewById(R.id.button3);
        fm1=new MyFragment();
        fm2 =new MyFragment2();
        father = new FatherFragment();
        fm = getSupportFragmentManager();

        FragmentTransaction transaction = fm.beginTransaction();
        transaction.add(R.id.fragmentlayout,father,"f2");
        transaction.addToBackStack("f2");

        transaction.commit();

//        FragmentTransaction transaction2 = fm.beginTransaction();
//        transaction2.add(R.id.fragmentlayout,fm1,"f1");
//        transaction2.addToBackStack("f1");
//        transaction2.commit();

        a2f1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle bundle = new Bundle();
                bundle.putString("MainActivity", "Hello,Fragment");
                //首先有一个Fragment对象 调用这个对象的setArguments(bundle)传递数据
                fm1.setArguments(bundle);
            }
        });
        a2f2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle bundle = fm1.getArguments();
                String result = bundle.getString("MyFragment");
                Toast.makeText(MainActivity.this, result, Toast.LENGTH_LONG).show();
            }
        });
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int backStackEntryCount = fm.getBackStackEntryCount();//栈中fragment个数
                fm.popBackStack();//回退一个
//                fm.popBackStackImmediate();//立刻回退一个

//                FragmentManager.BackStackEntry backStack = fm.getBackStackEntryAt(fm.getBackStackEntryCount() - 1);//获得当前fragment
//                String tag = backStack.getName();//fragment的tag
//                fm.popBackStack("tag",flags);
                fm.popBackStack("f1",1);
//                tag可以为null或者相对应的tag，flags只有0和1(POP_BACK_STACK_INCLUSIVE)两种情况
//                如果tag为null，flags为0时，弹出回退栈中最上层的那个fragment。
//                如果tag为null ，flags为1时，弹出回退栈中所有fragment。
//                如果tag不为null，那就会找到这个tag所对应的fragment，flags为0时，弹出该
//                fragment以上的Fragment，如果是1，弹出该fragment（包括该fragment）以
//                上的fragment。

            }
        });

    }
}
